package com.ffcs.crmd.tsp.admin.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import com.ffcs.crmd.tsp.admin.service.ITspClientService;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.core.util.PropertiesUtil;

/**
 * 
 * 功能说明:
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年2月16日 下午4:47:45
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
@Service
public class TspClientServiceImpl implements InitializingBean, DisposableBean, ITspClientService {
    
    private final Logger                  logger       = LoggerFactory.getLogger(LoggerName.ClientLoggerName);
    
    private static Map<String, TspClient> tspClientMap = new HashMap<String, TspClient>();
    
    @Override
    public void afterPropertiesSet() throws IOException {
        Properties properties = PropertiesUtil.getInstance().loadWeb("conf/cluster.properties");
        for (Object clusterName : properties.keySet()) {
            try {
                TspClient tspClient = new TspClient();
                tspClient.setServerAddr(properties.getProperty(String.valueOf(clusterName)));
                tspClient.setConnectTimeoutMillis(1500);
                tspClient.start();
                tspClientMap.put(String.valueOf(clusterName), tspClient);
            } catch (TspException e) {
                logger.error("tsp client service init exception:", e);
            }
        }
        
    }
    
    @Override
    public void destroy() {
        for (TspClient tspClient : tspClientMap.values()) {
            try {
                tspClient.shutdown();
            } catch (TspException e) {
                logger.error("tsp client service close exception:", e);
            }
        }
    }
    
    @Override
    public void add(String clusterName, String serverAddr) throws TspException {
        try {
            if(!tspClientMap.containsKey(clusterName)){
                TspClient tspClient = new TspClient();
                tspClient.setServerAddr(serverAddr);
                tspClient.setConnectTimeoutMillis(1500);
                tspClient.start();
                tspClientMap.put(clusterName, tspClient);
            }
        } catch (TspException e) {
            throw new TspException(e);
        }
    }
    
    
    @Override
    public void update(String clusterName, String serverAddr) {
        TspClient tspClient = tspClientMap.get(clusterName);
        tspClient.updateServerAddr(serverAddr);
    }
    
    @Override
    public void remove(String clusterName) throws TspException {
        try {
            TspClient tspClient = tspClientMap.remove(clusterName);
            if(tspClient != null){
                tspClient.shutdown();
            }
        } catch (TspException e) {
            throw new TspException(e);
        }
    }
    
    @Override
    public TspClient getClient(String clusterName){
        return tspClientMap.get(clusterName);
    }
    
    @Override
    public List<TspClient> getAllClient(){
        List<TspClient> tspClientList = new ArrayList<TspClient>();
        tspClientList.addAll(tspClientMap.values());
        return tspClientList;
    }
    
}
